package com.jvm.interpreter;

/**
 * @program: diy_simple_jvm
 * @description: 线程执行环境，虚拟机栈
 * @author: CaoYong
 * @create: 2021-07-09 13:45
 **/
public class ExecEnv {

    int top;
    Frame[] frames;

    public ExecEnv(int maxStack) {
        this.frames = new Frame[maxStack];
    }

    public void pushFrame(Frame frame) {
        if (top >= this.frames.length) {
            throw new IllegalStateException("stackoverflow");
        }
        frames[top++] = frame;
    }

    public Frame popFrame() {
        if (top < 1) {
            throw new IllegalStateException("empty stack");
        }
        Frame frame = frames[--top];
        frames[top] = null;
        return frame;
    }

    public boolean isEmpty() {
        return this.top == 0;
    }

    public Frame topFrame() {
        return this.frames[top - 1];
    }
}
